package com.simple.base.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.simple.util.common.Constants;
import com.simple.util.object.portal.SessionUserBean;

public class PasswordCheckInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		boolean interceptor = true;
		
		String goUrl = "";
		
		final String path = request.getServletPath();
		if(!("/".equals(path)||"/index".equals(path)||"/index/login".equals(path)||"/logout".equals(path)||"/message/alert".equals(path))){
			final SessionUserBean sessionUserBean= (SessionUserBean) request.getSession().getAttribute(Constants.CURRENT_SESSION_USERBEAN);
			if(sessionUserBean==null){
				goUrl = "/message/alert?errorId=message.system.need.login&goPage=/index";
				interceptor = false;
			}else{
				if(!("/user/self/editpassword".equals(path)||"/user/self/editPasswordSubmit".equals(path))){
					if(Constants.STATE_YES.equals(sessionUserBean.getUserInfoVO().getInitPasswordFlag())){
						goUrl = "/message/alert?errorId=message.user.modify.password&goPage=/user/self/editpassword";
						interceptor = false;
					}
				}
			}
		}
		
		if(!"".equals(goUrl)){
			response.sendRedirect(goUrl);
		}
		
		return interceptor;
	}
	
}
